package algorithm.combination;

import java.util.Arrays;
import java.util.function.Consumer;


public class Combination {
    int[] dataArray;

    public Combination(int[] dataArray) {
        this.dataArray = dataArray;
    }

    public void visit(int expectLength, Consumer<int[]> onVisit) {
        int[] visited = new int[dataArray.length];
        doVisit(expectLength, onVisit, visited, 0, 0);
    }

    private void doVisit(int expectLength, Consumer<int[]> onVisit, int[] context, int currentIndex, int toVisitIndex) {
        if (currentIndex == expectLength) {
            onVisit.accept(Arrays.copyOf(context, expectLength));
        } else {
            if(toVisitIndex<dataArray.length){
                context[currentIndex] = dataArray[toVisitIndex];
                doVisit(expectLength, onVisit, context, currentIndex + 1, toVisitIndex + 1);
                doVisit(expectLength, onVisit, context, currentIndex, toVisitIndex + 1);
            }
        }
    }
}
